import sqlite3 as lite
import sys

def SetupDB():

	con = None

	try:
	    con = lite.connect('chessboard.db')
	    
	    cur = con.cursor()
	    cmd = "create table Chessboard (colour text,piece text, Z int,X int,Y int)"    
	    cur.execute(cmd)
	    
	    data = cur.fetchone()
	    
	    print "SQLite version: %s" % data
	except lite.Error, e:
	    
	    print "Error %s:" % e.args[0]
	    sys.exit(1)
	finally:
	    
	    if con:
	        con.close()

def LayoutPieces():
    con = lite.connect('chessboard.db')        
    cur = con.cursor()
    with con:
        #chessboard 5 (top)
        cur.execute("INSERT INTO Chessboard VALUES('White','WRook1','5','1','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKnight1','5','2','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKing','5','3','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKnight2','5','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WRook2','5','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn1','5','1','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn2','5','2','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn3','5','3','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn4','5','4','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn5','5','5','2')")
        #chessboard 4
        cur.execute("INSERT INTO Chessboard VALUES('Black','BBishop1','4','1','1')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BUnicorn1','4','2','1')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BQueen','4','3','1')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BBishop2','4','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BUnicorn2','4','5','1')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BPawn6','4','1','2')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BPawn7','4','2','2')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BPawn8','4','3','2')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BPawn9','4','4','2')")
        cur.execute("INSERT INTO Chessboard VALUES('Black','BPawn10','4','5','2')")
        #
        #chessboard 3 middle (its empty to start with)
        #
        #chessboard 2
        cur.execute("INSERT INTO Chessboard VALUES('White','WBishop1','2','1','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WUnicorn1','2','2','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WQueen','2','3','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WUnicorn2','2','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKnight2','2','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn6','2','1','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn7','2','2','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn8','2','3','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn9','2','4','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn10','2','5','2')")
        #chessboard 1 (bottom)
        cur.execute("INSERT INTO Chessboard VALUES('White','WRook1','1','1','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKnight1','1','2','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKing','1','3','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WKnight2','1','4','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WRook2','1','5','1')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn1','1','1','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn2','1','2','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn3','1','3','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn4','1','4','2')")
        cur.execute("INSERT INTO Chessboard VALUES('White','WPawn5','1','5','2')")
        
    if con:
        con.close()
    return True
    
def getCoordinates(Piece):
    con = lite.connect('chessboard.db')
        
    cur = con.cursor()
    cmd1 = 'Select Z,X,Y Where piece=?',(Piece)
    
    cur.execute("SELECT Z,X,Y FROM Chessboard WHERE piece=(?)",(Piece,))
    data = cur.fetchone()
    print data
    return True

#time to list all the spaces which the currently held pieces can move
# current coordinates (coords) and returned are a 3 digit number
#move calculations are performed using mathmatical functions for addition and subtraction
#coordinates are represented as ZYX

def listRook(CurrentCoords):
    CC = CurrentCoords
    UpDown = []
    LeftRight = []
    ForwardBack = []
    
    CL1,CL2,CL3,CL4,CL5,CL6 = 111
    
    n1 = CC #+100 up
    n2 = CC #-100 down
    n3 = CC #-10 left
    n4 = CC #+10 right
    n5 = CC #+1 forward
    n6 = CC #-1 back
    
    while Cl1[1] >= 1 and Cl1[1] <= 5:
	for i in loop:
	    t = n1+100
	    CL1 = t
	    UpDown.append(t)

    while Cl2[1] >= 1 and Cl2[1] <= 5:
	for i in loop:
	    t = n2-100
	    CL2 = t
	    UpDown.append(t)
    
    while Cl3[2] >= 1 and Cl3[2] <= 5:
	for i in loop:
	    t = n3-10
	    CL3 = t
	    LeftRight.append(t)
    
    while Cl4[2] >= 1 and Cl4[2] <= 5:
	for i in loop:
	    t = n4+10
	    CL4 = t
	    LeftRight.append(t)
    
    while Cl5[3] >= 1 and Cl5[3] <= 5:
	for i in loop:
	    t = n5+1
	    CL5 = t
	    LeftRight.append(t)
    
    while Cl6[3] >= 1 and Cl6[3] <= 5:
	for i in loop:
	    t = n6-1
	    CL6 = t
	    LeftRight.append(t)
    return UpDown + ForwardBack + LeftRight
    
def listKnight(CurrentCoords):
    CC = CurrentCoords
    Moves = []
    Directions[90,99,101,110]
    CL1,CL2 = 111
    for i in Directions:
	CL1 = CC + i
	if CL1 >= 111 and CL1 <= 555:
	    Moves.append(CL1)
	CL2 = CC - i
	if CL2 >= 111 and CL2 <= 555:
	    Moves.append(CL2)
    return Moves

def listUnicorn(CurrentCoords):
    CC = CurrentCoords
    Up = []
    Down = []
    loop = range(1,5)
    CL1,CL2,CL3,CL4,CL5,CL6,CL7,CL8 = 111
    
    n1 = CC #+111 up top back right
    n2 = CC #+99 up top back left
    n3 = CC #+88 up top front left
    n4 = CC #+109 up top front right
    
    n5 = CC #-111 down top back left
    n6 = CC #-99 down top back right
    n7 = CC #-88 down top front right
    n8 = CC #-109 down top front left
    
    while Cl1 >= 111 and CL1 <= 555:
	for i in loop:
	    t = n1+111
	    CL1 = t
	    Up.append(t)
    
    while Cl2 >= 111 and CL2 <= 555:
	for i in loop:
	    t = n2+99
	    CL2 = t
	    Up.append(t)
	    
    while Cl3 >= 111 and CL3 <= 555:
	for i in loop:
	    t = n3+88
	    CL3 = t
	    Up.append(t)
    
    while Cl4 >= 111 and CL4 <= 555:
	for i in loop:
	    t = n4+109
	    CL4 = t
	    Up.append(t)
    while Cl5 >= 111 and CL5 <= 555:
	for i in loop:
	    t = n5-111
	    CL5 = t
	    Up.append(t)
    
    while Cl6 >= 111 and CL6 <= 555:
	for i in loop:
	    t = n6-99
	    CL6 = t
	    Up.append(t)
	    
    while Cl7 >= 111 and CL7 <= 555:
	for i in loop:
	    t = n7-88
	    CL7 = t
	    Up.append(t)
    
    while Cl8 >= 111 and CL8 <= 555:
	for i in loop:
	    t = n8-109
	    CL8 = t
	    Up.append(t)
	    
    return Up+Down

def listBishop(CurrentCoords):
    CC = CurrentCoords
    Up = []
    Down = []
    Diag =[]
    loop = range(1,5)
    CL1,CL2,CL3,CL4,CL5,CL6,CL7,CL8,CL9,CL10,CL11,CL12 = 111
    
    n1 = CC #+110 up  right
    n2 = CC #+90 up left
    n3 = CC #+99 up front 
    n4 = CC #+101 up back
    
    n5 = CC #-110 down left
    n6 = CC #-90 down right
    n7 = CC #-99 down back 
    n8 = CC #-101 down front
    
    n9 = CC #-11 front left
    n10 = CC #-9 front right
    n11 = CC #+11 back left
    n12 = CC #+9 back right
    
    while Cl1 >= 111 and CL1 <= 555:
	for i in loop:
	    t = n1+110
	    CL1 = t
	    Up.append(t)
    
    while Cl2 >= 111 and CL2 <= 555:
	for i in loop:
	    t = n2+90
	    CL2 = t
	    Up.append(t)
	    
    while Cl3 >= 111 and CL3 <= 555:
	for i in loop:
	    t = n3+99
	    CL3 = t
	    Up.append(t)
    
    while Cl4 >= 111 and CL4 <= 555:
	for i in loop:
	    t = n4+101
	    CL4 = t
	    Up.append(t)
    while Cl5 >= 111 and CL5 <= 555:
	for i in loop:
	    t = n5-110
	    CL5 = t
	    Down.append(t)
    
    while Cl6 >= 111 and CL6 <= 555:
	for i in loop:
	    t = n6-90
	    CL6 = t
	    Down.append(t)
	    
    while Cl7 >= 111 and CL7 <= 555:
	for i in loop:
	    t = n7-99
	    CL7 = t
	    Down.append(t)
    
    while Cl8 >= 111 and CL8 <= 555:
	for i in loop:
	    t = n8-101
	    CL8 = t
	    Down.append(t)
    while Cl9 >= 111 and CL9 <= 555:
	for i in loop:
	    t = n5+11
	    CL9 = t
	    Diag.append(t)
    
    while Cl10 >= 111 and CL10 <= 555:
	for i in loop:
	    t = n6+9
	    CL10 = t
	    Diag.append(t)
	    
    while Cl11 >= 111 and CL11 <= 555:
	for i in loop:
	    t = n7-11
	    CL11 = t
	    Diag.append(t)
    
    while Cl12 >= 111 and CL12 <= 555:
	for i in loop:
	    t = n8-9
	    CL12 = t
	    Diag.append(t)
    return Up+Down+Diag

def listQueen(CurrentCoords):
    Rook = listRook(CurrentCoords)
    Bishop = listBishop(CurrentCoords)
    Unicorn = listUnicorn(CurrentCoords)
    return Rook+Bishop+Unicorn

def listKing(CurrentCoords):
    CC = CurrentCoords
    K = listKnight(CurrentCoords)
    Directions = [90,99,101,110,89,91,100,109,111]
    Directions2 = [-1,1,-9,-10,-11,9,10,11]
    CL1,CL2,CL3 = 111
    for i in Directions:
	CL1 = CC + i
	if CL1 >= 111 and CL1 <= 555:
	    Moves.append(CL1)
	CL2 = CC - i
	if CL2 >= 111 and CL2 <= 555:
	    Moves.append(CL2)
	for i in Directions2:
	    CL3 = CC + i
	if CL3 >= 111 and CL3 <= 555:
	    Moves.append(CL3)
    return Moves
    
def listPawnMoveBlack(CurrentCoords):
    CC = CurrentCoords
    Moves = []
    Directioms = [-10,-100]
    CL1 = 111
    while CL1 >= 111 and CL1 <= 555:
	for i in Directions:
	    CL1 = CC + i
	    Moves.append(CL1)
    return Moves
    
def listPawnMoveWhite(CurrentCoords):
    CC = CurrentCoords
    Moves = []
    Directioms = [10,100]
    CL1 = 111
    while CL1 >= 111 and CL1 <= 555:
	for i in Directions:
	    CL1 = CC + i
	    Moves.append(CL1)
    return Moves
    

def listPawnAttackBlack():
    CC = CurrentCoords
    Moves = []
    Directions = [-111,-109,-11,-9]
    CL1 = 111
    while CL1 >= 111 and CL1 <= 555:
	for i in Directions:
	    CL1 = CC + i
	    Moves.append(CL1)
    return Moves

def listPawnAttackWhite():
    CC = CurrentCoords
    Moves = []
    Directions = [111,109,11,9]
    CL1 = 111
    while CL1 >= 111 and CL1 <= 555:
	for i in Directions:
	    CL1 = CC + i
	    Moves.append(CL1)
    return Moves

def movePiece(Piece,NewCoordinates):
    CoordZ = NewCoordinates[1]
    CoordX = NewCoordinates[2]
    CoordY = NewCoordinates[3]

    con = lite.connect('chessboard.db')        
    cur = con.cursor()
    with con:
        cur.execute("UPDATE Chessboard SET Z=(?),X=(?),Y=(?) WHERE piece=(?)",(Piece,CoordZ,CoordX,CoordY))
    if con:
        con.close()
    return True
